import VueRouter from "vue-router"
import About from "@/components/myRoute/part/About";
import Home from "@/components/myRoute/part/Home";
import News from "@/components/myRoute/part/News";
import Message from "@/components/myRoute/part/Message";
import MsgInfo from "@/components/myRoute/part/MsgInfo";

const router = new VueRouter({
    routes: [
        {
            path: '/about',
            component: About,
            meta:{
                title:'关于'
            }
        },
        {
            path: '/home',
            component: Home,
            meta:{
              title: '主页'
            },
            children: [
                {
                    path: 'news',
                    component: News,
                    meta: {
                        isAuth: true,
                        title: '新闻'
                    },
                    //独享守卫，beforeEach后的处理
                    beforeEnter:(to,form,next)=>{
                        console.log('beforeEnter',to,form)
                        next()
                    }
                },
                {
                    path: 'message',
                    component: Message,
                    meta: {
                        isAuth: true,
                        title:'消息'
                    },
                    children: [
                        {
                            name: 'info',
                            path: 'info/:id/:title',
                            component: MsgInfo,
                            meta:{
                              title:'详情'
                            },
                            props({params: {id, title}}) {
                                return {
                                    id,
                                    title
                                }
                            }
                        },
                    ]
                }
            ]
        }
    ]
})

//前置路由守卫
router.beforeEach((to, from, next) => {
    console.log('beforeEach', to, from)

    if (to.meta.isAuth) {
        if (localStorage.getItem('token') === '123456') {
            next()
        } else {
            alert('no auth')
        }
    }else {
        next()
    }

})
//后置路由守卫
router.afterEach((to, from) => {
    console.log('afterEach', to, from)
    document.title = to.meta.titel || '测试'
})


export default router
